package com.zx.zxojbackendjudge.strategy;



import com.zx.zxojbackendmodel.dto.judge.ExecuteCodeResponse;
import com.zx.zxojbackendmodel.dto.judge.JudgeInfo;
import com.zx.zxojbackendmodel.dto.question.JudgeCase;
import com.zx.zxojbackendmodel.dto.question.JudgeConfig;
import com.zx.zxojbackendmodel.enums.JudgeInfoMessageEnum;

import java.util.List;

/**
 * 默认判题策略
 */
public class DefaultStrategy implements Strategy{
    @Override
    public JudgeInfo doJudge(JudgeContext judgeContext) {
        List<String> inputs = judgeContext.getInputs();
        ExecuteCodeResponse executeCodeResponse = judgeContext.getExecuteCodeResponse();
        JudgeConfig judgeConfig = judgeContext.getJudgeConfig();
        List<JudgeCase> judgeCase = judgeContext.getJudgeCase();
        List<String> outputs = executeCodeResponse.getOutputList();
        JudgeInfo judgeInfo = executeCodeResponse.getJudgeInfo();
        judgeInfo.setMessage(JudgeInfoMessageEnum.ACCEPTED.getValue());

        //个数不一致，直接判错
        if(outputs.size() != inputs.size()){
            judgeInfo.setMessage(JudgeInfoMessageEnum.WRONG_ANSWER.getValue());
            return judgeInfo;
        }
        //逐个判断答案
        for (int i = 0; i < outputs.size(); i++) {
            if(!judgeCase.get(i).getOutput().equals(outputs.get(i))){
                judgeInfo.setMessage(JudgeInfoMessageEnum.WRONG_ANSWER.getValue());
                return judgeInfo;
            }
        }
        Long JAVA_PROGRAM_TIME_COST = 10000L;
        //判断内存
        if(judgeInfo.getMemory() == null || judgeInfo.getMemory() > judgeConfig.getMemoryLimit()){
            judgeInfo.setMessage(JudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED.getValue());
            return judgeInfo;
        }
        //判断时间
        if(judgeInfo.getTime() == null || (judgeInfo.getTime() - JAVA_PROGRAM_TIME_COST ) > judgeConfig.getTimeLimit()){
            judgeInfo.setMessage(JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED.getValue());
            return judgeInfo;
        }
        return judgeInfo;
    }
}
